<template>
    <div class="recover-password">
        <div class="body">
            <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="100px" class="demo-ruleForm">
                <el-form-item label="原密码" prop="oldPass">
                    <el-input type="password" v-model="ruleForm2.oldPass" autocomplete="off" size="mini" show-password></el-input>
                </el-form-item>
                <el-form-item label="新密码" prop="pass">
                    <el-input type="password" v-model="ruleForm2.pass" autocomplete="off" size="mini" show-password></el-input>
                </el-form-item>
                <el-form-item label="确认密码" prop="checkPass">
                    <el-input type="password" v-model="ruleForm2.checkPass" autocomplete="off" size="mini" show-password></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button size="small" @click="submitForm('ruleForm2')">提交</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>			
</template>

<script>
import axios from 'axios'
import qs from 'qs'
export default {
    data() {
      var checkOldpass = (rule, value, callback) => {
        if (!value) {
            return callback(new Error('原密码不能为空'));
        }else if(this.ruleForm2.resvalue=='20001'){
            callback(new Error('原密码错误'));
        }else{
            callback();
        }
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else if(value.length<6||value.length>14) {
          callback(new Error('密码长度应该在4到14位之间'));
          }else{
            callback();
          }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm2.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm2: {
          pass: '',
          checkPass: '',
          oldPass: '',
          resvalue:''
        },
        rules2: {
          pass: [
            { validator: validatePass, trigger: 'submit' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          oldPass: [
            { validator: checkOldpass, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            var postvalue = {
               oldPwd:this.ruleForm2.oldPass,
               newPwd:this.ruleForm2.pass
            };
            console.log(postvalue);
            axios.post('http://192.168.1.198:8080/partner/password', qs.stringify(postvalue)).then( res => {
                  // console.log(res);
                  if(res.status == 200){
                     console.log(res);
                      if(res.data.status == 'success'){
                          alert('修改成功!');
                          this.$router.replace('/login');
                      }else{
                          this.ruleForm2.resvalue= res.data.data.errorCode;
                          this.$refs[formName].validate();
                          this.ruleForm2.resvalue='';
                      }
                  }
              }).catch(res => {
                  console.log(res);
              })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
}
</script>

<style lang="scss">
    .recover-password{
        .body{
            width: 400px;
            margin-left: 200px;
            margin-top: 100px;

            .el-button{
                background-color: rgb(253, 135, 84);
                color: white;
                width: 100%;
            }
        }
    }
</style>
